module Main where 
import Entropy
import IO

parseBlameLine :: String -> (String, String)
parseBlameLine s = 
	let w = words s;
		w1 = w!!1; w2 = if (length w >2) then w!!2 else "\n"
	in (w1, w2)

parseBlame :: [String] -> [(String, String)]
parseBlame = map parseBlameLine

getRH :: Maybe FilePath -> IO Handle
getRH Nothing = return stdin
getRH (Just fn) = openFile fn ReadMode

getWH :: Maybe FilePath -> IO Handle
getWH Nothing = return stdout
getWH (Just fn) = openFile fn WriteMode

readLines :: Handle -> IO [String]
readLines = readLines' [] 
	where readLines' result h = 
		do { 
			line <- catch (hGetLine h) (\e -> return "");
			if (line == "") then return result else do readLines' (result++[line]) h
		}
			
printArray :: Show a => [a]->IO ()
printArray [] = return ()
printArray (a:as) = do
	putStrLn $ show a
	printArray as

apply x f = f $ x

meas :: [String]->Double
meas = fromIntegral . sum . map length

main = do
	input <- (readLines stdin) 
	inputData <- return $ parseBlame input
	result <- return $ map (apply inputData) [linesEntropy m, ragsEntropy m, actorsEntropy m, linesEntropy meas, ragsEntropy meas, actorsEntropy meas]
	printArray result

